import {Router} from "vue-router";
import store from "@/store";

export default {
  hooks(router: Router) {
    router.push = this.wrapFunction(router, 'push', {before: (route: any) => this.pushBufore(route, router)})
    //@ts-ignore
    router.goBackRefresh = () => this.goBackRefresh(router);
  },

  goBackRefresh(router: any) {
    store.commit('runtime/setReresh', true);
    router.go(-1);
  },

  pushBufore(route: any, router: any): void {
    //push的时候, 默认是true
    if (route.refresh === undefined || route.refresh) {
      store.commit('runtime/setReresh', true);
    }
  },

  wrapFunction(router: Router, method: string, callback: any) {
    //@ts-ignore
    let fun = router[method]
    return function (...args: any[]) {
      if (callback.before) {
        callback.before(...args);
      }
      let res = fun(...args);
      if (callback.after) {
        callback.after(res)
      }
      //@ts-ignore
      return res;
    };
  }
}